﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using ATMServiceLibrary.DTO;
using System.Runtime.Serialization;

namespace ATMServiceLibrary.DAO
{
    [DataContract]
    public class TaiKhoanDAO:DataProvider
    {

        public TaiKhoanDTO DangNhap(DangNhapDTO info)
        {
            try {
                connect();
                this.command = connection.CreateCommand();
                command.CommandText = "SELECT * FROM TAIKHOAN WHERE MaThe=@MaThe and MaPin=@MaPin";
                OleDbParameter para = command.Parameters.Add("@MaThe", OleDbType.VarWChar);
                para.Value = info.MaThe;

                para = command.Parameters.Add("@MaPin", OleDbType.VarWChar);
                para.Value = info.MaPin;
                OleDbDataReader reader= command.ExecuteReader();
                if (reader.Read())
                {
                    TaiKhoanDTO TaiKhoanDTO = new TaiKhoanDTO();    
                    TaiKhoanDTO.MaThe=reader["MaThe"].ToString();                    
                    TaiKhoanDTO.HoTen = reader["HoTen"].ToString();
                    TaiKhoanDTO.NgayHetHan = Convert.ToDateTime(reader["NgayHetHan"].ToString());
                    TaiKhoanDTO.SoDu = Convert.ToDouble(reader["SoDu"].ToString());
                    return TaiKhoanDTO;
                }
            }
            catch(Exception ex)
            {
                return null;
            }
            return null;
        }


        public TaiKhoanDTO LayThongTinTaiKhoanNhan(String MaThe)
        {
            try
            {
                connect();
                this.command = connection.CreateCommand();
                command.CommandText = "SELECT * FROM TAIKHOAN WHERE MaThe=@MaThe";
                OleDbParameter para = command.Parameters.Add("@MaThe", OleDbType.VarWChar);
                para.Value = MaThe;
                OleDbDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    TaiKhoanDTO TaiKhoanDTO = new TaiKhoanDTO();
                    TaiKhoanDTO.MaThe = reader["MaThe"].ToString();
                    TaiKhoanDTO.HoTen = reader["HoTen"].ToString();
                    TaiKhoanDTO.SoDu = Convert.ToDouble(reader["SoDu"].ToString());
                    return TaiKhoanDTO;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            return null;
        }

        public Double LaySoDuTaiKhoan(String MaThe)
        {
            try
            {
                connect();
                this.command = connection.CreateCommand();
                command.CommandText = "SELECT SoDu FROM TAIKHOAN WHERE MaThe=@MaThe";
                OleDbParameter para = command.Parameters.Add("@MaThe", OleDbType.VarWChar);
                para.Value = MaThe;
               
                OleDbDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    Double soDu = Convert.ToDouble(reader["SoDu"].ToString());
                    return soDu;
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                return -1;
            }
            return -1;
        }

        public void CapNhatSoDuTaiKhoan(String MaThe, Double soDu)
        {
            try {
                connect();
                StringBuilder queryStr = new StringBuilder(150);
                queryStr.Append("UPDATE TAIKHOAN SET SoDu=");
                queryStr.Append(soDu.ToString());
                queryStr.Append(" WHERE MaThe='");
                queryStr.Append(MaThe);
                queryStr.Append("'");
                
                this.command = new OleDbCommand(queryStr.ToString(),this.connection);                                    
                command.ExecuteNonQuery();

                this.connection.Close();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
